<?php
include("VerifySession.php");
?>
<?php
include("Header.php");
include("SNManager.class.php");
include("Pagination.php");

$Pagination = new Pagination();
$db = new SNDataManager();

$limit = $config['perpage'];
    $totalrows = mysql_fetch_array($db->GetCommentRowCount());
    
    if(isset($_REQUEST['page']) && is_numeric(trim($_REQUEST['page']))){$page = mysql_real_escape_string($_REQUEST['page']);}else{$page = 1;}
    $startrow = $Pagination->getStartRow($page,$limit);

    //create page links
    if($config['showpagenumbers'] == true)
    {
    	$pagination_links = $Pagination->showPageNumbers($totalrows['total'],$page,$limit);
    }else
    {
        $pagination_links=null;
    }
    
    if($config['showprevnext'] == true)
    {
    	$prev_link = $Pagination->showPrev($totalrows['total'],$page,$limit);
    	$next_link = $Pagination->showNext($totalrows['total'],$page,$limit);
    }else
    {
        $prev_link=null;$next_link=null;
    }
    

    if(!isset($_REQUEST['orderby']) OR trim($_REQUEST['orderby']) == "")
    {
        $result =$db->GetTopCommnet(1);
         if(mysql_num_rows($result) >0)
        {
        	$array = mysql_fetch_assoc($result);
        	//first field
        	$i = 0;
        	foreach($array as $key=>$value)
            {
        		if($i > 0){break;}else
                {
        		  $orderby=$key;
                }
        		$i++;		
        	}
        }
        $sort="ASC";    
    }else
    {
      $orderby=mysql_real_escape_string($_REQUEST['orderby']);
    }
    
    //IF SORT NOT SET OR VALID, SET DEFAULT
    if(!isset($_REQUEST['sort']) OR ($_REQUEST['sort'] != "ASC" AND $_REQUEST['sort'] != "DESC"))
    {
    	//default sort
    		$sort="ASC";
    	}else
        {	
    		$sort= mysql_real_escape_string($_REQUEST['sort']);
    }
if(!$orderby) $orderby="CREATE_DATE";

$SN_NUMBER;
$CONT_ID_NO;
$CONT_NAME;
$CONT_EMAIL;
$CONT_PHONE;
$COMMENT;
$CREATE_DATE;
$isSetViewID =false;
if($_REQUEST["VIEWID"])
{
    $comm= $db->FindComment($_REQUEST["VIEWID"]); 
    while ($row = mysql_fetch_array($comm, MYSQL_ASSOC))
    {
        $SN_NUMBER = $row['SERIAL_NUMBER'];
        $CONT_ID_NO = $row['ID_CARD_NO'];
        $CONT_NAME = $row['NAME'];
        $CONT_EMAIL = $row['EMAIL'];
        $CONT_PHONE = $row['PHONE'];
        $COMMENT = $row['COMMENT'];
        $CREATE_DATE= $row['CREATE_DATE'];
    } 
	$isSetViewID =true;
    unset($_REQUEST["VIEWID"]);
}
$allcomment_result =$db->getAllCommentPaging($orderby,$sort, $startrow,$limit);

?>         
<!-- Article -->
            <div id="adbox">
                <h2><span><a href="#">Comment List</a></span></h2>
                <table width="1024" border="1" align="center">
                 <!-- <tr>
                    <th scope="col">Date</th>
                    <th scope="col">Name</th>
                    <th scope="col">Serial Number</th>
                    <th scope="col">&nbsp;</th>
                  </tr>-->
<?php
 if(mysql_num_rows($allcomment_result) >0)
 {
         $array = mysql_fetch_assoc($allcomment_result);
            foreach ($array as $key=>$value) 
            {
               if($key!='ID')
                {
               
                	if($config['nicefields'])
                    {
                    	$field = str_replace("_"," ",$key);
                    	$field = ucwords($field);
                	}
                	
                	$field = $db->columnSortArrows($key,$field,$orderby,$sort);
                	echo "<th>" . $field . "</th>\n";
                 }
               }
            echo "<th>&nbsp;</th>\n";
            echo "</tr>\n";
        
        //reset result pointer
        mysql_data_seek($allcomment_result,0);
        
 while ($row = mysql_fetch_array($allcomment_result, MYSQL_ASSOC))
   { ?>
   <!-- CREATE DATE  SERIAL NUMBER  NAME  ID CARD NO  EMAIL  PHONE --> 
                  <tr>
                    <td><?php echo $row['CREATE_DATE']; ?></td>
                    <td><?php echo  $row['SERIAL_NUMBER']; ?></td>
                    <td><?php echo $row['NAME']; ?></td>
                    <td><?php echo  $row['ID_CARD_NO']; ?></td>
                    <td><?php echo  $row['EMAIL']; ?></td>
                    <td><?php echo  $row['PHONE']; ?></td>                    
                    <td align="center"><a href="CommentPage.php?VIEWID=<?php echo $row['ID']; ?>">VIEW</a></td>
                  </tr>
                
 <?php }
 } else {?> 
    <tr>
                    <td scope="col">CREATE DATE</td>
                    <td scope="col">NAME</td>
                    <td scope="col">SERIAL NUMBER</td>
                    <td scope="col">ID CARD NO</td>
                    <td scope="col">EMAIL</td>
                    <td scope="col">PHONE</td>
                    <td scope="col">&nbsp;</td>
                  </tr>
 <?php } ?>
  <th colspan="7" align="center" >
                <?php
            if(!($prev_link==null && $next_link==null && $pagination_links==null)){
            echo '<div class="pagination">';
            echo $prev_link;
            echo $pagination_links;
            echo $next_link;
            echo '<div style="clear:both;"></div>';
            echo "</div>";
            }
            ?>
    </th>
</table> 

         </p>
            </div> <!-- /article -->
            
<!-- Article -->
<?php if($isSetViewID){ ?>
             <div id="adbox" align="center">
                <h2><span><a href="#">View</a></span></h2>
                <form id="form2" name="form2" method="post" action="">
                  <table width="600" border="0" align="center" >
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Serial Number : </div></td>
                <td width="300"  class="entry" >
                <input name="txtRefSN" type="text" id="txtRefSN"  value="<?php echo $SN_NUMBER; ?>" readonly="readonly"  width="300" />
                  <font color="#FF0000">*</font></td>
                </tr>
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Date : </div></td>
                <td width="300"  class="entry" >
                <input name="txtDate" type="text" id="txtDate"  value="<?php echo $CREATE_DATE; ?>" readonly="readonly"  width="300" />
                  <font color="#FF0000">*</font></td>
                </tr>
                
              <tr>
                <td width="300" align="right" class="entry"><div align="right">ID Card/Passport No : </div></td>
                <td width="300"  class="entry" >
                <input name="txtIDCard" type="text" id="txtIDCard"  width="300" readonly="readonly" value="<?php echo $CONT_ID_NO; ?>" /></td>
                </tr>
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Name : </div> </td>
                <td width="300"  class="entry" >
                <input name="txtComName" type="text" id="txtComName"  width="300" readonly="readonly"  value="<?php echo $CONT_NAME; ?>" /> </td>
                </tr>
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Email : </div></td>
                <td width="300"  class="entry" >
                <input name="txtComEmail" type="text" id="txtComEmail"  width="300" readonly="readonly"  value="<?php echo $CONT_EMAIL; ?>" /></td>
                </tr>
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Tel : </div></td>
                <td width="300"  class="entry" >
                <input name="txtComTel" type="text" id="txtComTel"  width="300" readonly="readonly" value="<?php echo $CONT_PHONE; ?>"  /></td>
                </tr>
              <tr>
                <td width="300" align="right" class="entry"><div align="right">Comment :</div> </td>
                <td width="300"  class="entry" >
                  <label for="txtComment"></label>
                  <textarea name="txtComment" cols="50" rows="6" readonly="readonly" id="txtComment"><?php echo $COMMENT; ?></textarea></td></td>
                </tr>
               </table>
         </form>
            </div> <!-- /article -->
     <?php } ?>     
  
<?php
  include("Footer.php");
?>
